import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user.js'

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            name: 'menu',
            component: () => import('@/views/game/menu/MenuView.vue'),
            meta: { requestAuth: true },
        },
        {
            path: '/login/',
            name: 'login',
            component: () => import('@/views/user/account/LoginView.vue'),
            meta: { requestAuth: false },
        },
        {
            path: '/profile/',
            name: 'profile',
            component: () => import('@/views/user/account/ProfileView.vue'),
            meta: { requestAuth: true },
        },
        {
            path: '/register/',
            name: 'register',
            component: () => import('@/views/user/account/RegisterView.vue'),
            meta: { requestAuth: false },
        },
        {
            path: '/settings/',
            name: 'settings',
            component: () => import('@/views/game/settings/SettingsView.vue'),
            meta: { requestAuth: true },
        },
        {
            path: '/mode/',
            name: 'mode',
            component: () => import('@/views/game/mode/GameMode.vue'),
            meta: { requestAuth: true },
        },
        {
            path: '/pve/',
            name: 'pve',
            component: () => import('@/views/game/playground/pve/PvELevel.vue'),
            meta: { requestAuth: true },
        },
        {
            path: '/pve-playground/:level/',
            name: 'pve-playground',
            component: () => import('@/views/game/playground/pve/PvEPlayground.vue'),
            meta: { requestAuth: true },
        },
        {
            path: '/friend/',
            name: 'friend',
            component: () => import('@/views/user/friend/FriendView.vue'),
            meta: { requestAuth: true },
        },
        {
            path: '/chat/',
            name: 'chat',
            component: () => import('@/views/user/friend/ChatView.vue'),
            meta: { requestAuth: true },
        },
        {
            path: '/pk/',
            name: 'pk',
            component: () => import('@/views/game/playground/pvp/PkView.vue'),
            meta: { requestAuth: true },
        },
        {
            path: '/pvp-playground/',
            name: 'pvp-playground',
            component: () => import('@/views/game/playground/pvp/PvPPlayground.vue'),
            meta: { requestAuth: true },
        },
        {
            path: '/:pathMatch(.*)*',
            name: 'not-found',
            component: () => import('@/views/game/404/NotFound.vue'),
        },
    ],
})

router.beforeEach((to, from, next) => {
    const userStore = useUserStore()
    if (to.meta.requestAuth && !userStore.is_login) {
        next({ name: 'login' })
    } else {
        next()
    }
})

export default router
